<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="详细地址" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <el-button type="success" class="mr10" icon="el-icon-search" @click="func_handleAdd" plain>添加</el-button>
            </div>
			<el-table :data="memberAddressSubTableData" row-key="id" border  header-cell-class-name="table-header" class="table">
                <el-table-column prop="id" label="ID" width="55" align="center" v-if="false" />
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
                <el-table-column prop="linkman" label="联系人" />
                <el-table-column prop="phoneNumber" label="联系电话" />
                <el-table-column prop="detailAddress" label="详细地址" />
                <el-table-column prop="type" label="默认地址" >
                    <template #default="scope">
                        <el-tag :type=" scope.row.type === 1 ? 'success' : scope.row.type === 0 ? 'danger' : '' "> 默认地址 {{scope.row.type}} </el-tag>
                    </template></el-table-column>
				<el-table-column width="150">
					<template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handlerDel(scope.$index, scope.row)"> 删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item class="green" type="text" icon="el-icon-edit" @click="func_handleEdit(scope.$index, scope.row)">编辑地址</el-dropdown-item>
                                    <el-dropdown-item class="green" type="text" icon="el-icon-edit" @click="func_handleModifyDefault(scope.$index, scope.row)">设为默认</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageNum"
					:page-size="query.pageSize" :total="dataTotal" @current-change="func_handlePageChange"></el-pagination>
			</div>
        </div>
        <el-dialog title="编辑收件地址" v-model="editDialogVisible" width="55%" :close-on-click-modal="false">
            <div class="handle-box">
                <VMemberAddress v-bind:selectValue="VMemberData" @MemberAddressData="func_loadVMemberAddressData"/>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { onMounted, ref, reactive, toRefs, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findMemberAddressListPage, deleteMemberAddressById, deleteMemberAddress ,modifyDefaultAddress } from "../../../../api/hairmanager/memberAddress";

import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../../api/CommonErrorMessage.js";

import VMemberAddress from "./memberAddress.vue";

export default {
    name: "memberAddressSubTable",
    components: {VMemberAddress},
	props:{
		selectValue: Object
	},
	emits: ["memberAddressData"],
    setup(props, cex){
		onMounted(() => {
		});
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
        const loadFlg = ref(false);
        let query = reactive({
            memberId: 0,
            keyword: '',
            pageNum: 1,
            pageSize: 10,
        });
        const memberAddressSubTableData = ref([]);
        const dataTotal = ref(0);
		// =========================================================================================
        // 获取主表格数据
        const func_getData = () => {
            memberAddressSubTableData.value = [];
            findMemberAddressListPage(query).then((res)=>{
                memberAddressSubTableData.value = res.data.list;
                dataTotal.value = res.data.total;
            });
        };
        /**
         * @name 条件查询操作
         */
        const func_handleSearch = () => {
            query.pageNum = 1;
            func_getData();
        };
        /**
         * @name 主表格分页查询
         */
        const func_handlePageChange = (val) => {
            query.pageNum = val;
            func_getData();
        };
        const func_handlerDel = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {type: "warning",})
            .then(() => {
                deleteMemberAddressById(row['id']).then((res) => {
                    if(res.code != 200) {
                        if(res.type == 0) {
                            ElMessage.warning(res.message);
                        } else {
                            ElMessage.error(res.message);
                        }
                    } else {
                        ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                        memberAddressSubTableData.value.splice(index, 1);
                        func_getData();
                    }
                });
            }).catch(() => {});
        }
        // 扩展业务处理 ========================================================================================= START
        const VMemberData = reactive({
            load: false,
            memberId: 0,
            id: 0,
        });
        const editDialogVisible = ref(false);
        const func_loadVMemberAddressData = (v) => {
            if(v['state'] === 1) {
                editDialogVisible.value = false;
            } else {
                editDialogVisible.value = true;
            }
            func_getData();
        };
        /**
         * @name 添加数据
         */
        const func_handleAdd = () => {
            VMemberData['id'] = 0;            
            VMemberData['load'] = false;
            editDialogVisible.value = true;
        };
        /**
         * @name 编辑数据
         */
        const func_handleEdit = (index,row) => {
            VMemberData['id'] = row['id'];
            VMemberData['load'] = true;
            editDialogVisible.value = true;
        };
        /**
         * @name 设定默认选择数据
         */
        const func_handleModifyDefault = (index, row) => {
            let _param = {id: row['id']};
            modifyDefaultAddress(_param).then((res) => {
                func_getData();
            });
        };        
        // 扩展业务处理 ========================================================================================= END
        // =========================================================================================================
        // 业务数据监听
        watch(() => props.selectValue, (val) => {
            query.memberId = val['id'];
            VMemberData['memberId'] = val['id'];
            val['load'];
            func_getData()
        },{deep: true, immediate: true});
        return {
            VMemberData, query, memberAddressSubTableData, dataTotal, 
            func_getData, func_handleSearch, func_handlePageChange, func_handleAdd, func_handleEdit, func_handlerDel, func_handleModifyDefault,
            editDialogVisible, VMemberData, func_loadVMemberAddressData, 
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 300px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.blue {
    color: #1684FC;
}
.mr10 {
    margin-right: 10px;
}
</style>